#!/usr/bin/env python
#-*- coding:utf-8 -*-
import pymysql,pymongo,time
# connect to mysql database
mysql = pymysql.connect(host='f2326m3431.iok.la', database='bopu_dev', user='root', password='HBatmMysql2019')
cursor = mysql.cursor()

#connect to mongodb and obtain total lines in mysql
myclient = pymongo.MongoClient("mongodb://f2326m3431.iok.la:27017/")
mongo = myclient["bjtu"]
mongo.authenticate('bjtu_dev',password='bjtu_know2020')

cursor.execute('SELECT max(account) FROM sys_user')
countlines = cursor.fetchall()
count = countlines.__len__()

#count = 300
print(count)

i = 0
j = 100
start_time = time.time()
#select from mysql to insert mongodb by 100 lines.
for i in range(0,count,100):
    #print(a,b)
    #print(i)
    #print('SELECT * FROM quiz_submission where quiz_submission_id > %d and  quiz_submission_id <= %d' %(i,j))
    submission = mysql.query(f'SELECT * FROM sys_user where id > %d and  id <= %d' %(i,j))
    #print(submission)
    if submission:
        #collection_name like mysql sys_user
        mongo.collection_name.insert_many(submission)
    else:
        i +=100
        j +=100
        continue
    i +=100
    j +=100
end_time = time.time()
deltatime = end_time - start_time
totalhour = int(deltatime / 3600)
totalminute = int((deltatime - totalhour * 3600) / 60)
totalsecond = int(deltatime - totalhour * 3600 - totalminute * 60)
#print(migrate data total time consuming.)
print("Data Migrate Finished,Total Time Consuming: %d Hour %d Minute %d Seconds" %(totalhour,totalminute,totalsecond))

cursor.close()
mysql.close()